﻿using Microsoft.AspNetCore.Identity;
using SheepStorage.Domain.DTO.InputDto;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.BaseImp;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    public class PermissionService: IPermissionService
    {
        //角色菜单权限表
        private readonly IBaseRepository<MenuRolePermission> MRPbaseRepository;

        //权限表
        private readonly IBaseRepository<Permission> PermissionbaseRepository;
        //角色信息
        private readonly IBaseRepository<Role> Rolerepository;
        //菜单信息
        private readonly IBaseRepository<MenuList> menuListRepository;

        public PermissionService(IBaseRepository<MenuRolePermission> MRPbaseRepository, IBaseRepository<Permission> PermissionbaseRepository, IBaseRepository<Role> Rolerepository, IBaseRepository<MenuList> menuListRepository)
        {
            this.MRPbaseRepository = MRPbaseRepository;
            this.PermissionbaseRepository = PermissionbaseRepository;
            this.Rolerepository = Rolerepository;
            this.menuListRepository = menuListRepository;
        }

        //显示角色菜单权限表
        public object GetMenuRolePermissions()
        {
            //var list = MRPbaseRepository.GetAll().ToList();
            //菜单名称   角色名称(集合)   权限集合

            var lists = (
                     from menu in menuListRepository.GetAll()
                     join mrplist in MRPbaseRepository.GetAll()
                     on menu.MenuId equals mrplist.MenuId
                     
                     select new
                     {
                         menu.MenuName,
                         RoleName=(
                             from role in Rolerepository.GetAll()
                             join mrp in MRPbaseRepository.GetAll()
                             on role.RoleId equals mrp.RoleId
                            select new
                            {
                                role.RoleName
                            }



                         ).Distinct().ToList(),
                         Permissname = (
                           from permission in PermissionbaseRepository.GetAll()
                           join mrp in MRPbaseRepository.GetAll()
                           on permission.PermissionId equals mrp.PermissionId
                          
                           select new
                           {
                               permission.PermissionName
                           }
                          ).Distinct().ToList()
                     }


                ).ToList();

            var lists1 = (
                  from menu in menuListRepository.GetAll()
                  join mrplist in MRPbaseRepository.GetAll()
                  on menu.MenuId equals mrplist.MenuId

                  join role in Rolerepository.GetAll()
                  on mrplist.RoleId equals role.RoleId

                  join permission in PermissionbaseRepository.GetAll()
                  on mrplist.PermissionId equals permission.PermissionId
                  
                 

                  select new
                  {
                      menu.MenuName,
                      role.RoleName,
                      permission.PermissionName
                  }                 

             


             ).ToList();



            return lists1;
        }


        //查询显示权限表
        public List<Permission> GetPermission()
        {
            var list = PermissionbaseRepository.GetAll().ToList();
            return list;
        }


        //批量添加角色菜单权限表
        public int AddMenuRolePermission(MenuRolePermissionInput input)
        {
            //先删除原有数据
            var oldlist = MRPbaseRepository.GetAll().Where(c => c.MenuId == input.MenuId).ToList();
            MRPbaseRepository.DeleteRange(oldlist);

            //批量添加新权限数据
            foreach (var item in input.PermissionIdList)
            {
                foreach(var item1 in input.RoleIdList)
                {
                    var menupermiss = new MenuRolePermission()
                    {
                        MenuId = input.MenuId,
                        PermissionId = item,
                        PermissionName=input.PermissionName,
                        RoleId=item1
                    };
                    MRPbaseRepository.Add(menupermiss);
                }
               
             //   MRPbaseRepository.Add(menupermiss);
            }
            return 1;
        }
               

            
         
    }
}
